home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / PC100.C < prev    next >
Text File  |  1993-08-09  |  13KB  |  484 lines

  1. /* Interface driver for the PACCOMM PC-100 board for the IBM PC */
  2. /* UNFINISHED, DOESN'T WORK YET - work in progress by Bdale */
  3. /* currently only attempting to use the AMD7910 on Channel A */
  4.  
  5. #include <stdio.h>
  6. #include <dos.h>
  7. #include "global.h"
  8. #include "config.h"
  9. #ifdef PC100
  10. #include "mbuf.h"
  11. #include "iface.h"
  12. #include "pktdrvr.h"
  13. #include "netuser.h"
  14. #include "pc100.h"
  15. #include "n8530.h"
  16. #include "ax25.h"
  17. #include "trace.h"
  18. #include "pc.h"
  19.  
  20. static void hspint __ARGS((struct hdlc *hp));
  21. static void hexint __ARGS((struct hdlc *hp));
  22. static void hrxint __ARGS((struct hdlc *hp));
  23. static void htxint __ARGS((struct hdlc *hp));
  24. static void rts __ARGS((int16 base,int x));
  25. static void hdlcparam __ARGS((struct hdlc *hp));
  26. static int pc_raw __ARGS((struct iface *iface,struct mbuf *bp));
  27. static int pc_stop __ARGS((struct iface *iface,int temp));
  28.  
  29. static struct pc100 Pc100[NPC];
  30. static INTERRUPT (*Pchandle[])() = { pc0vec };
  31. static struct hdlc Hdlc[2*NPC];
  32. static int16 Npc;
  33.  
  34. /* Branch table for interrupt handler */
  35. static void (*Svec[]) __ARGS((struct hdlc *hp)) = {
  36.     htxint, hexint, hrxint, hspint
  37. };
  38.  
  39. /* Master interrupt handler for the PC-100 card. All interrupts come
  40.  * here first, then are switched out to the appropriate routine.
  41.  */
  42. void
  43. pcint(dev)
  44. int dev;
  45. {
  46.     char iv;
  47.     struct hdlc *hp;
  48.  
  49.     int16 pcbase = Pc100[dev].addr;
  50.     Pc100[dev].ints++;
  51.  
  52.     /* Read interrupt vector, including status, from channel B */
  53.     iv = read_scc(CTL+pcbase+CHANB,R2);
  54.  
  55.     hp = &Hdlc[2 * dev + ((iv & 0x80)? 0 : 1)];
  56.  
  57.     /* Now switch to appropriate routine */
  58.     (*Svec[(iv>>1) & 0x3])(hp);
  59.  
  60.     /* Reset interrupt pending state (register A only) */
  61.     write_scc(CTL+pcbase+CHANA,R0,RES_H_IUS);
  62.  
  63.     /* Wang the 8530 hardware interrupt acknowledge line - Bdale */
  64.     inportb(pcbase+INTACK);
  65. }
  66. /* HDLC Special Receive Condition interrupt
  67.  * The most common event that triggers this interrupt is the
  68.  * end of a frame; it can also be caused by a receiver overflow.
  69.  */
  70. static void
  71. hspint(hp)
  72. struct hdlc *hp;
  73. {
  74.     char c = read_scc(CTL+hp->base,R1);    /* Fetch latched bits */
  75.  
  76.     hp->spints++;
  77.  
  78.     if((c & (END_FR|CRC_ERR)) == END_FR && hp->rcvbuf != NULLBUF
  79.         && hp->rcvbuf->cnt > 1){
  80.         /* End of valid frame */
  81.         hp->rcvbuf->cnt--;    /* Toss 1st crc byte */
  82.         enqueue(&hp->rcvq,hp->rcvbuf);
  83.         hp->rcvbuf = NULLBUF;
  84.         hp->rcvcnt++;
  85.     } else {
  86.         /* An overflow or CRC error occurred; restart receiver */
  87.         hp->crcerr++;
  88.         if(hp->rcvbuf != NULLBUF){
  89.             hp->rcp = hp->rcvbuf->data;
  90.             hp->rcvbuf->cnt = 0;
  91.         }
  92.     }
  93.     write_scc(CTL+hp->base,R0,ERR_RES);
  94. }
  95. /* HDLC SIO External/Status interrupts
  96.  * The only one of direct interest is a receiver abort; the other
  97.  * usual cause is a change in the modem control leads, so kick the
  98.  * transmit interrupt routine.
  99.  */
  100. static void
  101. hexint(hp)
  102. struct hdlc *hp;
  103. {
  104.     hp->exints++;
  105.     hp->status = read_scc(CTL+hp->base,R0);    /* Fetch status */
  106.     if((hp->status & BRK_ABRT) && hp->rcvbuf != NULLBUF){
  107.         hp->aborts++;
  108.         /* Restart receiver */
  109.         hp->rcp = hp->rcvbuf->data;
  110.         hp->rcvbuf->cnt = 0;
  111.     }
  112.     write_scc(CTL+hp->base,R0,RES_EXT_INT);
  113.     write_scc(CTL+hp->base,R0,RES_H_IUS);
  114.     /* Kick the transmit interrupt routine for a possible modem change */
  115.     htxint(hp);
  116. }
  117. /* HDLC receiver interrupt handler. Allocates buffers off the freelist,
  118.  * fills them with receive data, and puts them on the receive queue.
  119.  */
  120. static void
  121. hrxint(hp)
  122. struct hdlc *hp;
  123. {
  124.     struct mbuf *bp;
  125.     int16 base = hp->base;
  126.  
  127.     hp->rxints++;
  128.  
  129.     /* Allocate a receive buffer if not already present */
  130.     if((bp = hp->rcvbuf) == NULLBUF){
  131.         bp = hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  132.         if(bp == NULLBUF){
  133.             /* No memory, abort receiver */
  134.             hp->nomem++;
  135.             write_scc(CTL+base,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  136.             (void) inportb(base+DATA);
  137.             return;
  138.         }
  139.         hp->rcp = hp->rcvbuf->data;
  140.     }
  141.     while(read_scc(CTL+base,R0) & Rx_CH_AV){
  142.         if(bp->cnt++ >= hp->bufsiz){
  143.             /* Too large; abort the receiver, toss buffer */
  144.             hp->toobig++;
  145.             write_scc(CTL+base,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  146.             (void) inportb(base+DATA);
  147.             free_p(bp);
  148.             hp->rcvbuf = NULLBUF;
  149.             break;
  150.         }
  151.         /* Normal save */
  152.         *hp->rcp++ = inportb(base+DATA);
  153.     }
  154. }
  155. static int ctswait;
  156. /* HDLC transmit interrupt service routine
  157.  *
  158.  * The state variable tstate, along with some static pointers,
  159.  * represents the state of the transmit "process".
  160.  */
  161. static void
  162. htxint(hp)
  163. struct hdlc *hp;
  164. {
  165.     int c;
  166.     int16 base = hp->base;
  167.     hp->txints++;
  168.  
  169.     DISABLE();
  170.     while(read_scc(CTL+base,R0) & Tx_BUF_EMP){
  171.         switch(hp->tstate){
  172.         /* First here for efficiency */
  173.         case ACTIVE:        /* Sending frame */
  174.             if((c = PULLCHAR(&hp->sndbuf)) != -1){
  175.                 outportb(base+DATA,c);
  176.             } else {
  177.                 /* Do this after sending the last byte */
  178.                 write_scc(CTL+base,R0,RES_Tx_P);
  179.                 if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF){
  180.                     switch(hp->mode){
  181.                     case CSMA:
  182.                         /* Begin transmitter shutdown */
  183.                         hp->tstate = FLUSH;
  184.                         break;
  185.                     case FULLDUP:
  186.                         hp->tstate = IDLE;
  187.                         break;
  188.                     }
  189.                 }
  190.             }
  191.             continue;
  192.         case IDLE:
  193.             /* Transmitter idle. Find a frame for transmission */
  194.             if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF)
  195.                 goto ret;
  196.  
  197.         case DEFER:    /* note fall-thru */
  198.             if(hp->mode == CSMA && (hp->status & DCD)){
  199.                 hp->tstate = DEFER;
  200.                 goto ret;
  201.             }
  202.             rts(base,ON);    /* Transmitter on */
  203.         case KEYUP:    /* note fall-thru */
  204.             if((hp->status & CTS) == 0){
  205.                 ctswait++;
  206.                 hp->tstate = KEYUP;
  207.                 goto ret;
  208.             }
  209.             write_scc(CTL+base,R0,RES_Tx_CRC);
  210.             c = PULLCHAR(&hp->sndbuf);
  211.             outportb(hp->base+DATA,c);
  212.             hp->tstate = ACTIVE;
  213.             write_scc(CTL+base,R0,RES_EOM_L);
  214.             continue;
  215.         case FLUSH:    /* Sending flush character */
  216.             outportb(hp->base+DATA,0);
  217.             hp->tstate = FIN2;
  218.             continue;
  219.         case FIN2:
  220.             write_scc(CTL+base,R0,SEND_ABORT);
  221.             hp->tstate = IDLE;
  222.             rts(base,OFF);
  223.             write_scc(CTL+base,R0,RES_Tx_P);
  224.             continue;
  225.         }
  226.     }
  227. ret:    RESTORE();
  228. }
  229.  
  230. /* Set request-to-send on modem */
  231. static void
  232. rts(base,x)
  233. int16 base;
  234. int x;
  235. {
  236.     int16 cmd;
  237.  
  238.     if(x)
  239.         cmd = TxCRC_ENAB | RTS | TxENAB | Tx8 | DTR;
  240.     else
  241.         cmd = TxCRC_ENAB | TxENAB | Tx8 | DTR;
  242.     write_scc(CTL+base,R5,cmd);
  243. }
  244.  
  245. /* (re)Initialize HDLC controller parameters */
  246. static void
  247. hdlcparam(hp)
  248. struct hdlc *hp;
  249. {
  250.     int16 tc;
  251.  
  252.     /* Initialize 8530 channel for SDLC operation */
  253.     int16 base = hp->base;
  254.  
  255.     DISABLE();
  256.     switch(base & 2){
  257.     case 0:
  258.         write_scc(CTL+base,R9,CHRA);    /* Reset channel A */
  259.         break;
  260.     case 2:
  261.         write_scc(CTL+base,R9,CHRB);    /* Reset channel B */
  262.         break;
  263.     }
  264.     /* Wait/DMA disable, Int on all Rx chars + spec condition,
  265.      * parity NOT spec condition, TxINT enable, Ext Int enable
  266.      */
  267.     write_scc(CTL+base,R1,INT_ALL_Rx | TxINT_ENAB | EXT_INT_ENAB);
  268.  
  269.     /* Dummy interrupt vector, will be modified by interrupt type
  270.      * (This probably isn't necessary)
  271.      */
  272.     write_scc(CTL+base,R2,0);
  273.  
  274.     /* 8 bit RX chars, auto enables off, no hunt mode, RxCRC enable,
  275.      * no address search, no inhibit sync chars, enable RX
  276.      */
  277.     write_scc(CTL+base,R3,Rx8|RxCRC_ENAB|RxENABLE);
  278.  
  279.     /* X1 clock, SDLC mode, Sync modes enable, parity disable
  280.      * (Note: the DPLL does a by-32 clock division, so it's not necessary
  281.      * to divide here).
  282.      */
  283.     write_scc(CTL+base,R4,X1CLK | SDLC | SYNC_ENAB);
  284.  
  285.     /* DTR On, 8 bit TX chars, no break, TX enable, SDLC CRC,
  286.      * RTS off, TxCRC enable
  287.      */
  288.     write_scc(CTL+base,R5,DTR|Tx8|TxENAB|TxCRC_ENAB);
  289.  
  290.     /* SDLC flag */
  291.     write_scc(CTL+base,R7,FLAG);
  292.  
  293.     /* No reset, status low, master int enable, enable lower chain,
  294.      * no vector, vector includes status
  295.      */
  296.     write_scc(CTL+base,R9,MIE|NV|VIS);
  297.     /* CRC preset 1, NRZI encoding, no active on poll, flag idle,
  298.      * flag on underrun, no loop mode, 8 bit sync
  299.      */
  300.     write_scc(CTL+base,R10,CRCPS|NRZI);
  301.  
  302.     /* Board no longer channel-specific for clk.  The board should be set
  303.      * up to run from the 4.9152Mhz onboard crystal connected to PCLK.
  304.      * Both channels get receive clock at 32x from PCLK via the DPLL,
  305.      * with TRxC as an output, via a 4040 div by 32 counter to RTxC set
  306.      * us as an input to provide the transmit clock.
  307.      */
  308.  
  309.     /*            TRxC = BR Generator Output, TRxC O/I,
  310.      *          transmit clock = RTxC pin,
  311.      *          receive clock = DPLL output
  312.      */
  313.     write_scc(CTL+base,R11,TRxCBR|TRxCOI|TCRTxCP|RCDPLL);
  314.  
  315.     /* Compute and load baud rate generator time constant
  316.      * DPLL needs x32 clock
  317.      * XTAL is defined in pc100.h to be the crystal clock / (2 * 32)
  318.      */
  319.     tc = XTAL/(hp->speed) - 2;
  320.     write_scc(CTL+base,R12,tc & 0xff);
  321.     write_scc(CTL+base,R13,(tc >> 8) & 0xff);
  322.  
  323.     write_scc(CTL+base,R14,SNRZI);    /* Set NRZI mode */
  324.     write_scc(CTL+base,R14,SSBR);    /* Set DPLL source = BR generator */
  325.     write_scc(CTL+base,R14,SEARCH);    /* Enter search mode */
  326.     /* Set baud rate gen source = PCLK, enable baud rate gen */
  327.     write_scc(CTL+base,R14,BRENABL|BRSRC);
  328.  
  329.     /* Break/abort IE, TX EOM IE, CTS IE, no SYNC/HUNT IE, DCD IE,
  330.      * no Zero Count IE
  331.      */
  332.     write_scc(CTL+base,R15,BRKIE|TxUIE|CTSIE|DCDIE);
  333.  
  334.     RESTORE();
  335.     if(hp->mode == FULLDUP){
  336.         rts(base,ON);
  337.     } else if(hp->tstate == IDLE){
  338.         rts(base,OFF);
  339.     }
  340. }
  341. /* Attach a PC-100 interface to the system
  342.  * argv[0]: hardware type, must be "pc100"
  343.  * argv[1]: I/O address, e.g., "0x380"
  344.  * argv[2]: vector, e.g., "2"
  345.  * argv[3]: mode, must be:
  346.  *        "ax25" (AX.25 UI frame format)
  347.  * argv[4]: interface label, e.g., "pc0"
  348.  * argv[5]: receiver packet buffer size in bytes
  349.  * argv[6]: maximum transmission unit, bytes
  350.  * argv[7]: interface speed, e.g, "9600"
  351.  *  notused:
  352.  * argv[8]: First IP address, optional (defaults to Ip_addr)
  353.  * argv[9]: Second IP address, optional (defaults to Ip_addr)
  354.  */
  355. int
  356. pc_attach(argc,argv,p)
  357. int argc;
  358. char *argv[];
  359. void *p;
  360. {
  361.     struct iface *if_pca,*if_pcb;
  362.     struct hdlc *hp;
  363.     int dev;
  364.  
  365.     if(*Mycall == '\0') {
  366.         tputs(Nomycall);
  367.         return -1;
  368.     }
  369.     if(stricmp(argv[3],"AX25") != 0) {
  370.         tputs("Mode must be ax25\n");
  371.         return -1;
  372.     }
  373.     if(if_lookup(argv[4]) != NULLIF){
  374.         tprintf(Ifexist,argv[4]);
  375.         return -1;
  376.     }
  377.     if(Npc >= NPC){
  378.         tprintf("Max %d pc100 controller/s\n",NPC);
  379.         return -1;
  380.     }
  381.  
  382.     dev = Npc++;
  383.  
  384.     /* Initialize hardware-level control structure */
  385.     Pc100[dev].addr = htoi(argv[1]);
  386.     Pc100[dev].vec = htoi(argv[2]);
  387.     /* Initialize modems */
  388.     outportb(Pc100[dev].addr + MODEM_CTL,0x22);
  389.  
  390.     /* Save original interrupt vector */
  391.     Pc100[dev].oldvec = getirq(Pc100[dev].vec);
  392.     /* Set new interrupt vector */
  393.     if(setirq(Pc100[dev].vec,Pchandle[dev]) == -1){
  394.         tprintf("IRQ %u out of range\n",Pc100[dev].vec);
  395.         Npc--;
  396.         return -1;
  397.     }
  398.     /* Create interface structures and fill in details */
  399.     if_pca = mxallocw(sizeof(struct iface));
  400.     if_pcb = mxallocw(sizeof(struct iface));
  401.  
  402.     if_pca->addr = Ip_addr;
  403.     if_pcb->addr = Ip_addr;
  404.     if_pca->name = strxdup(argv[4]);
  405.     if_pca->name = if_name(if_pca,"a");
  406.     if_pcb->name = strxdup(argv[4]);
  407.     if_pcb->name = if_name(if_pcb,"b");
  408. /*    if_pcb->name[strlen(argv[4]) - 1]++;    /* kludge */
  409.     if_pcb->mtu = if_pca->mtu = atoi(argv[6]);
  410.     if_pcb->type = if_pca->type = CL_AX25;
  411.     if_pca->dev = 2*dev;
  412.     if_pcb->dev = 2*dev + 1;
  413.     if_pcb->stop = if_pca->stop = pc_stop;
  414.     if_pcb->output = if_pca->output = ax_output;
  415.     if_pcb->raw = pc_raw;
  416.  
  417.     if_pcb->send = if_pca->send = ax_send;
  418.  
  419.     if_pcb->hwaddr = strxdup(Mycall);
  420.     if_pca->hwaddr = strxdup(Mycall);
  421.  
  422.     if_pca->next = if_pcb;
  423.     if_pcb->next = Ifaces;
  424.     Ifaces = if_pca;
  425.     if_pca->niface = Niface++;
  426.     if_pcb->niface = Niface++;
  427.  
  428.     hp = &Hdlc[2*dev+1];
  429.     hp->speed = (int16)atol(argv[7]);
  430.     hp->base = Pc100[dev].addr + CHANB;
  431.     hp->bufsiz = atoi(argv[5]);
  432.     hdlcparam(hp);
  433.  
  434.     hp = &Hdlc[2*dev];
  435.     hp->speed = (int16)atol(argv[7]);
  436.     hp->base = Pc100[dev].addr + CHANA;
  437.     hp->bufsiz = atoi(argv[5]);
  438.     hdlcparam(hp);
  439.  
  440.     /* Clear mask (enable interrupt) in 8259 interrupt controller */
  441.     clrbit(INTMASK,(1 << Pc100[dev].vec));
  442.  
  443.     return 0;
  444. }
  445. static int
  446. pc_stop(iface,temp)
  447. struct iface *iface;
  448. int temp;
  449. {
  450.     int dev = iface->dev;
  451.  
  452.     if(dev & 1)
  453.         return 0;
  454.     dev >>= 1;    /* Convert back into PC100 number */
  455.     /* Turn off interrupts */
  456.     maskoff(Pc100[dev].vec);
  457.  
  458.     /* Restore original interrupt vector */
  459.     setirq(Pc100[dev].vec,Pc100[dev].oldvec);
  460.  
  461.     /* Force hardware reset */
  462.     write_scc(CTL+Pc100[dev].addr + CHANA,R9,FHWRES);
  463.     return 0;
  464. }
  465.  
  466. /* Send raw packet on PC-100 */
  467. static int
  468. pc_raw(iface,bp)
  469. struct iface *iface;
  470. struct mbuf *bp;
  471. {
  472.     char kickflag;
  473.     struct hdlc *hp;
  474.  
  475.     dump(iface,IF_TRACE_OUT,CL_AX25,bp);
  476.     hp = &Hdlc[iface->dev];
  477.     kickflag = (hp->sndq == NULL);
  478.     enqueue(&hp->sndq,bp);
  479.     if(kickflag)
  480.         htxint(&Hdlc[iface->dev]);
  481.     return 0;
  482. }
  483.  
  484. #endif /* PC100 */